package com.oa.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.oa.aop.LogAnnotation;
import com.oa.bean.Notice;
import com.oa.service.impl.NoticeServiceImpl;

@Controller
public class NoticeController {
	@Resource
	NoticeServiceImpl noticeServiceImpl;

/**
 * 
 * @param file
 * @param notice
 * @param session
 * @return
 * 上传附件命名规则： 时间戳
 */
	@RequestMapping("/addNotice")
	public String addNotice(MultipartFile file,Notice notice,HttpSession session){
		System.out.println(file);
		System.out.println(notice);
		System.out.println(file.getOriginalFilename());
		if(!"".equals(file.getOriginalFilename())){
			String fileName = file.getOriginalFilename();
			String prefix = fileName.substring(fileName.lastIndexOf("."));
			Date date = new Date();
			String newFileName = date.getTime()+prefix;
			String uploadPath="E:\\javaEE\\OA-SSM\\src\\main\\webapp\\attachment\\"+newFileName;//设置上传路径
			System.out.println("上传路径"+uploadPath);
			try {
				file.transferTo(new File(uploadPath));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			notice.setNappendix(newFileName);
		}
		notice.setPublisher_id((Integer)session.getAttribute("uid"));
		notice.setDepart_id((Integer)session.getAttribute("deptno"));
		Integer result = noticeServiceImpl.addNotice(notice);
		System.out.println(result);
		return "redirect:Template/notice-manage.jsp";
	}
	
	@RequestMapping("getNoticeList")
	@ResponseBody
	@LogAnnotation(description="查看公告")
	public Map<String,Object> getNoticeList(Integer currentPage,HttpSession session){
		System.out.println(currentPage);
		Integer deptno = (Integer) session.getAttribute("deptno"); 
		System.out.println("部门编号是"+deptno);
		Map<String,Object> map = noticeServiceImpl.getNoticeList(currentPage,deptno);
		return map;
	}
	
	@RequestMapping("/downNoticeFile")
	public String downNoticeFile(String fileName,HttpServletResponse response){
		System.out.println(fileName);
		if(!"null".equals(fileName)){
			response.setCharacterEncoding("UTF-8");
			response.setContentType("multipart/form-data");
			String realPath = "E:\\javaEE\\OA-SSM\\src\\main\\webapp\\attachment\\"+fileName;
			try {
				response.setHeader("Content-Disposition", "attachment;fileName="+ new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
				OutputStream os = response.getOutputStream();
				os.write(FileUtils.readFileToByteArray(new File(realPath)));
				os.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return null;
	}
	
	@RequestMapping("/deleteNotice")
	@ResponseBody
	public String deleteNotice(Integer nid){
		System.out.println(nid);
		String result = noticeServiceImpl.deleteNotice(nid);
		System.out.println(result);
		return result;
	}
	
	@RequestMapping("lookDetails")
	public String lookDetails(Integer nid,Model model){
		System.out.println(nid);
		Notice notice = noticeServiceImpl.lookNoticeDetails(nid);
		model.addAttribute("notice",notice);
		return "Template/notice-details";
	}

}
